/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved.
 */

package com.huawei.config;

import com.huawei.service.ThirdPartyApiService;
import com.huawei.vo.ApiResponse;
import io.modelcontextprotocol.server.McpServer;
import io.modelcontextprotocol.server.McpServerFeatures;
import io.modelcontextprotocol.server.McpSyncServer;
import io.modelcontextprotocol.server.McpSyncServerExchange;
import io.modelcontextprotocol.server.transport.WebMvcSseServerTransportProvider;
import io.modelcontextprotocol.spec.McpSchema;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;
import java.util.function.BiFunction;

/**
 * mcp工具
 *
 * @since 2025.10.11
 */
@Configuration
public class McpTools {

    @Bean(destroyMethod = "close")
    public McpSyncServer mcpSyncServer(WebMvcSseServerTransportProvider transportProvider,
                                       ThirdPartyApiService thirdPartyApiService) {
        McpSyncServer mcpSyncServer = McpServer.sync(transportProvider)
            .serverInfo("my-server", "1.0.0")
            .capabilities(McpSchema.ServerCapabilities.builder()
                .tools(true)
                .logging()
                .build())
            .build();


        // 新增：Matrix指定模块的总数
        String matrixJsonSchema = """  
            {  
                "type":"object",  
                "properties":{  
                    "serviceType" : {  
                        "type": "string",  
                        "examples": ["model", "dataset", "registry", "agent", "knowledge"]
                    }  
                },  
                "required":[  
                    "serviceType"  
                ],  
                "additionalProperties":false
            }  
            """;

        BiFunction<McpSyncServerExchange, Map<String, Object>, McpSchema.CallToolResult> matrixCall =
            (exchange, arguments) -> {
                ApiResponse response = thirdPartyApiService.getMatrix(
                    String.valueOf(arguments.get("serviceType")));

                String resultText = response.isSuccess() ?
                    response.getData().toString() :
                    "获取信息失败: " + response.getMessage();

                return new McpSchema.CallToolResult(resultText, false);
            };

        mcpSyncServer.addTool(new McpServerFeatures.SyncToolSpecification(
            new McpSchema.Tool("getMatrixServiceTotal",
                "查询Matrix服务已上架的服务总数,参数可选[model, dataset, registry, agent, knowledge]",
                matrixJsonSchema),
            matrixCall
        ));

        // 新增：汇率查询工具
        String exchangeJsonSchema = """  
            {  
                "type":"object",  
                "properties":{  
                    "fromCurrency" : {  
                        "type": "string",  
                        "description": "源货币代码，如: USD, CNY"  
                    },
                    "toCurrency" : {  
                        "type": "string",  
                        "description": "目标货币代码，如: USD, CNY"  
                    }
                },  
                "required":[  
                    "fromCurrency", "toCurrency"
                ],  
                "additionalProperties":false
            }  
            """;

        BiFunction<McpSyncServerExchange, Map<String, Object>, McpSchema.CallToolResult> exchangeCall =
            (exchange, arguments) -> {
                ApiResponse response = thirdPartyApiService.getExchangeRate(
                    String.valueOf(arguments.get("fromCurrency")),
                    String.valueOf(arguments.get("toCurrency")));

                String resultText = response.isSuccess() ?
                    response.getData().toString() :
                    "获取汇率信息失败: " + response.getMessage();

                return new McpSchema.CallToolResult(resultText, false);
            };

        mcpSyncServer.addTool(new McpServerFeatures.SyncToolSpecification(
            new McpSchema.Tool("getExchangeRate", "获取货币汇率信息,参数可选如: USD, CNY", exchangeJsonSchema),
            exchangeCall
        ));

        return mcpSyncServer;
    }
}
